<template>
  <div>
    <FormPanel ref="from" :list="formList" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <div style="height:47px;"></div>
    <BaseDialog :visible.sync="issearch" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ownerSeacher @close="searchclose" />
    </BaseDialog>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <!-- <el-button @click="closeDrawer(2)" type="primary">保 存</el-button> -->
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)">保 存</multiuse-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getDetail, postSave } from '@/api/omSale/OrderManagement/PickAddress/index.js';
import ownerSeacher from './ownerSeachers';
import { phoneAndSolidify } from '@/utils/validate.js';
export default {
  data() {
    this.saveData = null;
    return {
      formList: this.createdFrom(),
      issearch: false,
      dealerCode: ''
    };
  },
  props: ['rowData'],
  mixins: [dictionary],
  created() {
    this.getDetail(this.rowData);
  },
  components: {
    ownerSeacher
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请选择',
          readonly: true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          },
          rules: [{ required: true, message: '请选择', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '联系人',
          fieldName: 'linkMan',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '地址性质',
          fieldName: 'addressProperty',
          placeholder: '请选择',
          itemList: this.createDictList('6035'),
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '联系电话',
          fieldName: 'linkPhone',
          placeholder: '请输入',
          rules: [
            { required: true, message: '请输入', trigger: 'blur' },
            {
              validator: phoneAndSolidify,
              trigger: 'blur'
            }
          ]
        },
        {
          type: 'SELECT',
          label: '省份',
          fieldName: 'provinceId',
          placeholder: '请选择',
          itemList: this.createRegionsList(),
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          change: p => {
            if (p) {
              this.formList.map(x => {
                if (x.fieldName == 'cityId') {
                  x.itemList = [];
                  x.initialValue = '';
                }
                if (x.fieldName == 'districtId') {
                  x.itemList = [];
                  x.initialValue = '';
                }
                if (x.fieldName == 'accountAddress') {
                  x.initialValue = '';
                }
              });
              this.formList.map(x => {
                if (x.fieldName == 'cityId') {
                  x.itemList = this.createcityList(p);
                }
              });
            } else {
              this.formList.map(x => {
                if (x.fieldName == 'cityId') {
                  x.itemList = [];
                  x.initialValue = '';
                }
                if (x.fieldName == 'districtId') {
                  x.itemList = [];
                  x.initialValue = '';
                }
                if (x.fieldName == 'accountAddress') {
                  x.initialValue = '';
                }
              });
            }
          }
        },
        {
          type: 'SELECT',
          label: '城市',
          fieldName: 'cityId',
          placeholder: '请选择',
          itemList: [],
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          change: p => {
            this.setAddress(p);
            if (p) {
              this.formList.map(x => {
                if (x.fieldName == 'districtId') {
                  x.itemList = [];
                  x.initialValue = '';
                }
              });
              this.formList.map(x => {
                if (x.fieldName == 'districtId') {
                  x.itemList = this.district(p);
                }
              });
            } else {
              this.formList.map(x => {
                if (x.fieldName == 'districtId') {
                  x.itemList = [];
                  x.initialValue = '';
                }
              });
            }
          }
        },
        {
          type: 'SELECT',
          label: '县区',
          fieldName: 'districtId',
          placeholder: '请选择',
          itemList: [],
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'SELECT',
          label: '状态',
          fieldName: 'status',
          placeholder: '请选择',
          itemList: this.createDictList('1003'),
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'MULTIPLE_SELECT',
          label: '地址类型',
          fieldName: 'addressTypeList',
          placeholder: '请选择',
          itemList: this.createDictList('6008'),
          rules: [{ required: true, message: '请选择', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '详细地址',
          fieldName: 'address',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }],
          selfCols: 2
        },
        {
          type: 'INPUT',
          label: '结算目的地',
          fieldName: 'accountAddress',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        },
        {
          type: 'TEXT_AREA',
          label: '备注',
          fieldName: 'remark',
          placeholder: '请输入',
          selfCols: 3
        }
      ];
    },
    async getDetail(data) {
      let res = await getDetail({ addressId: data.addressId });
      for (let i in res.data) {
        this.formList.map(x => {
          if (i == x.fieldName) {
            x.initialValue = res.data[i];
          }
        });
      }
      this.dealerCode = data.dealerCode;
      this.formList.find(x => x.fieldName == 'cityId').itemList = this.createcityList(data.provinceId);
      this.formList.find(x => x.fieldName == 'districtId').itemList = this.district(data.cityId);
    },
    seacherFun() {
      this.issearch = true;
    },
    changeHandle(val) {
      this.saveData = val;
    },
    searchclose(val) {
      if (val) {
        this.formList.find(x => x.fieldName == 'dealerName').initialValue = val.dealerName;
        this.dealerCode = val.dealerCode;
      }
      this.issearch = false;
    },
    setAddress(val) {
      if (val) {
        let str = this.formList.find(x => x.fieldName == 'provinceId').initialValue;
        let list = this.createcityList(str);
        console.log(list);
        let value = '';
        if (list.length > 0) {
          list.map(x => {
            if (x.value == val) {
              value = x.text;
            }
          });
          console.log(value);
          this.formList.map(x => {
            if (x.fieldName == 'accountAddress') {
              console.log(value);
              x.initialValue = value;
            }
          });
        }
      } else {
        this.formList.map(x => {
          if (x.fieldName == 'accountAddress') {
            x.initialValue = '';
          }
        });
      }
    },
    async closeDrawer(type) {
      if (type == 1) {
        this.$emit('close');
      } else {
        this.$refs.from.SUBMIT_FORM();
        console.log(this.saveData, 'this.saveData');
        if (this.saveData) {
          let json = JSON.parse(JSON.stringify(this.saveData));
          json.addressId = this.rowData.addressId;
          json.dealerCode = this.dealerCode;
          let res = await postSave(json);
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '保存成功',
              type: 'success'
            });
            this.$emit('close', json);
          } else {
            this.saveData = null;
          }
        }
      }
    }
  }
};
</script>
